package com.fengjing.launcher.common;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fengjing.launcher.R;
import com.fengjing.launcher.model.FilmModel;

import java.io.File;
import java.util.List;

/**
 * Created by admin on 2017/9/21.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

    private List<FilmModel> filmModels;

    private int childMarginTop = 0;
    private int childMarginBottom = 0;

    private int numColumns = 0;

    public void setNumColumns(int numColumns) {
        this.numColumns = numColumns;
    }

    public void setFilmModels(List<FilmModel> filmModels) {
        this.filmModels = filmModels;
    }

    @Override
    public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(-2, -2);
        View view = View.inflate(parent.getContext(), R.layout.film_item_layout, null);
        view.setLayoutParams(marginLayoutParams);
        return new MyAdapter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyHolder holder, int position) {

        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();

        marginLayoutParams.topMargin = 0;
        marginLayoutParams.bottomMargin = 0;

        if (position < numColumns){
            marginLayoutParams.topMargin = childMarginTop;
        }else if (position > getItemCount() - 1 - numColumns){
            marginLayoutParams.bottomMargin = childMarginBottom;
        }

        holder.itemView.setLayoutParams(marginLayoutParams);

        holder.itemView.setOnFocusChangeListener(new MyOnFocusChanageListener());
        holder.itemView.setOnClickListener(new MyOnClickListener(filmModels.get(position)));

        ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.icon);
        TextView textView = (TextView) holder.itemView.findViewById(R.id.text);

        String pngPath =  filmModels.get(position).getFilmPNG();

        if (pngPath != null){
            Glide.with(holder.itemView.getContext()).load(new File(filmModels.get(position).getFilmPNG())).into(imageView);
        }else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.category_item_pic1).into(imageView);
        }

        textView.setText(filmModels.get(position).getFilmName());
    }

    @Override
    public int getItemCount() {
        if (filmModels == null) {
            return 0;
        }
        return filmModels.size();
    }

    public void setChildMarginTop(int childMarginTop) {
        this.childMarginTop = childMarginTop;
    }

    public void setChildMarginBottom(int childMarginBottom) {
        this.childMarginBottom = childMarginBottom;
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        public MyHolder(View itemView) {
            super(itemView);
        }
    }
}
